package com.nova.scenic.projectlibs.util;

import android.content.Context;
import android.renderscript.Sampler.Value;

public class MResourceUtil {

	public static int getLayoutId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString, "layout",  
                paramContext.getPackageName());  
    }  

    public static int getStringId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString, "string",  
                paramContext.getPackageName());  
    }  

    public static int getDrawableId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString,  
                "drawable", paramContext.getPackageName());  
    }  

    public static int getStyleId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString,  
                "style", paramContext.getPackageName());  
    }  

    public static int getId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString,  
                "id", paramContext.getPackageName());  
    }  

    public static int getColorId(Context paramContext, String paramString) {  
        return paramContext.getResources().getIdentifier(paramString,  
                "color", paramContext.getPackageName());  
    }  
	
    public static int getValue(Context context,String packageAndName){
    	try {
			String valuse[] = packageAndName.split("\\.");
			Class desireClass = getDeclareClass(context, valuse[1]);
			int vlaue =	(Integer) desireClass.getField(valuse[2]).get(desireClass);
			return vlaue;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
	public static int[] getIdsByName(Context context, String className, String name) {
		int[] ids = null;
		try {
			Class desireClass = getDeclareClass(context, className);
			Object nameClass = desireClass.getField(name).get(desireClass) ;
			if ((desireClass != null) && (nameClass!= null) && (nameClass.getClass().isArray())){
				ids = (int[]) desireClass.getField(name).get(desireClass);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		return ids;
	}

	private static Class getDeclareClass(Context context, String className) throws ClassNotFoundException {
		String packageName = context.getPackageName();
		Class r = null;
		int[] ids = null;
		r = Class.forName(packageName + ".R");

		Class[] classes = r.getClasses();
		Class desireClass = null;

		for (int i = 0; i < classes.length; ++i) {
			if (classes[i].getName().split("\\$")[1].equals(className)) {
				desireClass = classes[i];
				break;
			}
		}
		return desireClass;
	}
}
